/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package intelligenttm.ITMCarto;

import Data.ThematicData;
import intelligenttm.SourceFile.FenJiCartoType;
import intelligenttm.SourceFile.RibbonShow;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Rectangle;
import java.util.ArrayList;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import me.aviator.entity.HibernateUtil;
import me.aviator.entity.Ribbon;
import me.aviator.entity.myColor;
import me.aviator.service.impl.RibbonService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * FXML Controller class
 *
 * @author 望舒凌霄
 */
public class SHierarchicalStatisticalGraphController implements Initializable {
    @FXML
    private AnchorPane RootPane;
    @FXML
    private Button BtnCancel;
    @FXML
    private Button BtnLast;
    @FXML
    private Button BtnNext;
    @FXML
    private TextField LevelNumTex;
    @FXML
    private ChoiceBox<String> AttributePup;
    @FXML
    private TableView<FenJiCartoType> TV;
    @FXML
    private TableColumn<FenJiCartoType, String> BPT;
    @FXML
    private TableColumn<FenJiCartoType, String> EPT;
    @FXML
    private TableColumn<FenJiCartoType, Rectangle> CLT;
    @FXML
    private TableView<RibbonShow> ColorView;
    @FXML
    private TableColumn<RibbonShow, HBox> ColorRView;
    
    private List<BaseData> BDList=null;
    private String AttributeString=null;
    private int LevelNum=4;
    private ObservableList<FenJiCartoType> FenJiCartoTypeItemList =  FXCollections.observableArrayList();
    private int WhichBtn=0;
    private List<FenJiCartoType> FenJiColorList;
    private Ribbon RibbonColor=null;
    private List<Ribbon> RList=new ArrayList<>();
    private RibbonService ribbonService;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        SessionFactory sf=HibernateUtil.getSessionFactory();
        Session sess=sf.openSession();
        Transaction tx=sess.beginTransaction();
        
        ribbonService=new RibbonService(sess);
        
        CLT.setCellValueFactory(new PropertyValueFactory<>("RectangleBox"));
        BPT.setCellValueFactory(new PropertyValueFactory<>("BeginNum"));
        BPT.setCellFactory(TextFieldTableCell.forTableColumn());
        BPT.setOnEditCommit((CellEditEvent<FenJiCartoType, String> t) -> {
            ((FenJiCartoType) t.getTableView().getItems().get(
                    t.getTablePosition().getRow())
                    ).setBeginNum(t.getNewValue());
        });
        EPT.setCellValueFactory(new PropertyValueFactory<>("EndNum"));
        EPT.setCellFactory(TextFieldTableCell.forTableColumn());
        EPT.setOnEditCommit((CellEditEvent<FenJiCartoType, String> t) -> {
            ((FenJiCartoType) t.getTableView().getItems().get(
                    t.getTablePosition().getRow())
                    ).setEndNum(t.getNewValue());
        });
        SetColorRibbon();
    }    
    
    public void SetBaseData(List<BaseData> tBDList){
        BDList=tBDList;
        ObservableList<String> ALIST=FXCollections.observableArrayList();
        for(int i=0;i<BDList.size();i++){
            ThematicData TD= BDList.get(i).getData();
            List<String>Title=TD.GetTitleString();
            for (int j=0;j<Title.size();j++){
                ALIST.add(Title.get(j));
            }
        }
        AttributePup.setItems(ALIST);
        AttributePup.getSelectionModel().select(0); 
        AttributeString=AttributePup.getItems().get(0);
        AttributePup.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{ 
            AttributeString=AttributePup.getItems().get(newv.intValue());
            AttributePup.getSelectionModel().select(newv.intValue());
        });
    }
    
    public void SetColorRibbon(){
        RList=ribbonService.ListByNum(LevelNum);
        ObservableList<RibbonShow> RibbonShowList =  FXCollections.observableArrayList();
        RibbonShowList.clear();
        ColorRView.setCellValueFactory(new PropertyValueFactory<>("CHB"));
        for (int i=0;i<RList.size();i++){
            RibbonShow RS=new RibbonShow(RList.get(i));
            RibbonShowList.add(RS);
        }
        ColorView.setItems(RibbonShowList);
        ColorView.getSelectionModel().select(0);
        
        SetLevelData();
    }
    
    public void SetLevelData(){
        FenJiCartoTypeItemList.clear();
        List<myColor> MYCL=RList.get(ColorView.getSelectionModel().getSelectedIndex()).getColorlist();
        for(int i=0;i<LevelNum;i++){
            FenJiCartoType FJCT=new FenJiCartoType("0","0",MYCL.get(i));
            FenJiCartoTypeItemList.add(FJCT);
        }
        TV.setItems(FenJiCartoTypeItemList);
        
    }
    
    @FXML
    private void HandleBtnCancel(ActionEvent event) {
        WhichBtn=0;
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnLast(ActionEvent event) {
        WhichBtn=-1;
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnNext(ActionEvent event) {
        
        RibbonColor=RList.get(ColorView.getSelectionModel().getSelectedIndex());
//        FenJiColorList.clear();
//        for(int i=0;i<FenJiCartoTypeItemList.size();i++){
//            FenJiColorList.add(FenJiCartoTypeItemList.get(i));
//        }
        WhichBtn=1;
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleGetLevelNum(KeyEvent event) {
        LevelNum=Integer.parseInt(LevelNumTex.getText());
        SetColorRibbon();
    }
    @FXML
    private void HandleCTV(MouseEvent event) {
        SetLevelData();
    }
    
    
    

    public String getAttributeString() {
        return AttributeString;
    }

    public void setAttributeString(String AttributeString) {
        this.AttributeString = AttributeString;
    }

    public int getLevelNum() {
        return LevelNum;
    }

    public void setLevelNum(int LevelNum) {
        this.LevelNum = LevelNum;
    }

    public List<FenJiCartoType> getFenJiColorList() {
        return FenJiColorList;
    }

    public void setFenJiColorList(List<FenJiCartoType> FenJiColorList) {
        this.FenJiColorList = FenJiColorList;
    }

    public int getWhichBtn() {
        return WhichBtn;
    }

    public Ribbon getRibbonColor() {
        return RibbonColor;
    }
    
}
